package com.myapp.ruyixing.ui;

import java.util.List;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.ShopMoreInfo;
import com.myapp.ruyixing.model.ShopMoreInfo.ShopMoreInfoItem;
import com.myapp.ruyixing.ui.widget.TitleBar;

public class ShopTrafficAndTimeActivity extends BaseActivity {

	private ListView mListView;
	private TextView mEmptyView;

	private RYHttpResponseHandler mHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ShopMoreInfo data = (ShopMoreInfo) o;

			if (data != null && data.data != null) {
				MyListAdapter adapter = new MyListAdapter(data.data);
				mListView.setAdapter(adapter);
			}
			mListView.setEmptyView(mEmptyView);
			((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_traffic_and_time);

		initView();

		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);

		String shopid = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_ID);
		String shopName = getIntent().getStringExtra(
				ShopDetailActivity.INTENT_SHOP_NAME);
		if (TextUtils.isEmpty(shopid) == true) {
			Toast.makeText(getApplicationContext(), "商户为空", Toast.LENGTH_SHORT)
					.show();
		} else {
			RYHttpClient.getShopTrafficAndTime(this, mHandler, shopid);
		}

		if (TextUtils.isEmpty(shopid) == false) {
			((TitleBar) findViewById(R.id.title_bar)).setTitleText("商户详情信息\n"
					+ shopName);
		}
	}

	private void initView() {
		mListView = (ListView) findViewById(android.R.id.list);
		mEmptyView = (TextView) findViewById(android.R.id.empty);

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});
	}

	private class ViewHolder {
		TextView mContentView;
		TextView mTitleView;
	}

	private class MyListAdapter extends ArrayAdapter<ShopMoreInfoItem> {

		public MyListAdapter(List<ShopMoreInfoItem> list) {
			super(ShopTrafficAndTimeActivity.this, -1, list);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getContext()).inflate(
						R.layout.cell_shop_traffic_and_time, parent, false);

				final ViewHolder holder = new ViewHolder();

				holder.mContentView = (TextView) convertView
						.findViewById(R.id.content);
				holder.mTitleView = (TextView) convertView
						.findViewById(R.id.title);

				convertView.setTag(holder);
			}

			ViewHolder holder = (ViewHolder) convertView.getTag();
			ShopMoreInfoItem item = getItem(position);

			if (item != null) {
				holder.mTitleView.setText(item.title);
				holder.mContentView.setText(item.content);
			} else {
				holder.mTitleView.setText("");
				holder.mContentView.setText("");
			}

			return convertView;

		}
	}
}
